/*
 * @Author: 陈宇 975155172@qq.com
 * @Date: 2024-08-14 10:58:52
 * @LastEditors: 陈宇 975155172@qq.com
 * @LastEditTime: 2024-08-26 14:45:42
 * @FilePath: \blog-web\src\stores\modules\user\index.ts
 * @Description: 用户信息
 */
import { defineStore } from 'pinia';
import {
  getUserInfo,
} from '@/api/stores/index'; // 请求信息
import { removeRouteListener } from '@/utils/route-listener';
import type { UserState } from './types';

const useUserStore = defineStore('user', {
  state: (): UserState => ({
    role: '',
    username: undefined,
    email: undefined,
    nickname: undefined,
    portrait: undefined,
    user_tel: undefined,
    article_num: undefined,

    ICP_record_number: undefined,
    headline: undefined,
    subtitle: undefined,
    logoImg: undefined,
    web_name: undefined,
  }),

  getters: {},

  actions: {
    // Set user's information
    setInfo(partial: Partial<UserState>) {
      this.$patch(partial);
    },

    // Reset user's information
    resetInfo() {
      this.$reset();
    },

    // Get user's information
    async info() {
      const res = await getUserInfo();

      this.setInfo(res.data);
    },

    logoutCallBack() {
      this.resetInfo();
      removeRouteListener();
    },
  },
});

export default useUserStore;
